<?php
  include('lock.php');
  //session_destroy();
  
  function products()
  {
    $q=mysql_query('SELECT demo_ID, demo_name, demo_description, demo_url, demo_price FROM demo ORDER BY demo_ID DESC');
    if(mysql_num_rows($q) == 0)
    {
      echo"There are no products to display!";
    }
    else
    {
      while($data_fetch=mysql_fetch_array($q))
      {      
        echo"<table>";
        echo"<tr>"."<td>". "Name: ".$data_fetch['demo_name']."</td>"."</tr>";
        echo"<tr>"."<td>". "Description:" .$data_fetch['demo_description']."</td>"."</tr>";
        echo"<tr>"."<td>". "Price per item: "." $".number_format($data_fetch['demo_price'],2)."</td>";
        echo"<tr>"."<td>". "URL: " .$data_fetch['demo_url']."</td>"."</tr>";
?>  
        <tr>
        <td>
        <form name="add" action="add2.php" method="post">
          <input type="hidden" name="addID" value="<?php echo $data_fetch['demo_ID'];?>" />
          <input type="submit" onClick="window.location.href=window.location.href" name="add" value="Add"/>
        </form>
       
        <form name="remove" action="remove2.php" method="post">
          <input type="hidden" name="removeID" value="<?php echo $data_fetch['demo_ID'];?>" />
          <input type="submit" onClick="window.location.href=window.location.href" value="Remove"/>
        </form>
        </td>

<?php
        echo "</tr>";
        echo"<table>";
      }
    }
  }
  
  function cart()
  {
    global $login_session;
    global $demo_id;
    global $total;
    echo"<p><strong>Reservation Details:</strong></p>";
     
    $q=mysql_query("SELECT demo_ID, COUNT(demo_ID) FROM cart WHERE user_ID='$login_session' GROUP BY demo_ID");
    
    while($data=mysql_fetch_array($q))
    {
      $num_demo_id=$data['COUNT(demo_ID)'];
      $demo_id=$data['demo_ID'];
      $ql=mysql_query("SELECT * FROM demo WHERE demo_ID='$demo_id'");
      
      while($data_fetch=mysql_fetch_array($ql))
      {
        $demo_price=number_format($data_fetch['demo_price'],2);
        $demo_name=$data_fetch['demo_name'];
        $sub =$demo_price *  $num_demo_id;
        
        echo"<table>";
        echo"<tr>"."<td>";
        echo $demo_name. ' x ' . $num_demo_id . ' @ $'. $demo_price. ' = ' . '$'. number_format($sub, 2). '<br/>';
        echo"</td>";
?>        
            <td>
              <form name="delete" action="delete2.php" method="post">
                <input type="hidden" name="deleteID" value="<?php echo $data_fetch['demo_ID'];?>" />
                <input type="submit" onClick="window.location.href=window.location.href" value="Delete"/>
              </form>
            </td>
          </tr>
        </table>
        
<?php        
      }
     $total+=$sub;
    }
    
    if($total==0)
    {
      echo "<br/>";
      echo "Your Shopping Cart is Empty!"; 
    }
    else
    {
      echo "<p> Total Price: $" .number_format($total,2) . "</p>";    
    }
  }
?>